package cn.xopencode.appletweb.controller.passport;
import cn.hutool.core.date.DateTime;
import cn.xopencode.common.framework.util.HttpUtil;
import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.oss.rpc.passport.PassPortRpc;
import cn.xopencode.oss.rpc.passport.vo.*;
import cn.xopencode.security.annotations.RequiresNone;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import static cn.xopencode.common.framework.vo.CommonResult.success;

@Api(tags = "用户登录相关API")
@RestController
@RequestMapping("/passport")
public class PassportController {
    @DubboReference(version = "${dubbo.consumer.PassPortRpc.version}")
    private PassPortRpc passPortRpc;
    @PostMapping("/login-by-mobile")
    @ApiOperation("手机密码登陆")
    @RequiresNone
    public CommonResult<PassportAccessTokenRespVO> loginByMobile(@Valid PassportLoginByMobileReqVO loginByMobileDTO,
                                                                 HttpServletRequest request) {

        return success(new PassportAccessTokenRespVO().setAccessToken(StringUtils.uuid(true)).setRefreshToken(StringUtils.uuid(true)).setExpiresTime(new DateTime().toJdkDate()),"手机号为"+loginByMobileDTO.getMobile()+"的员工登录成功。");
    }

    @PostMapping("/login-by-username")
    @ApiOperation("用户名密码登陆")
    @RequiresNone
    public CommonResult<PassportAccessTokenRespVO> loginByUserName(@Valid PassportLoginByUserNameReqVO loginByUserNameDTO,
                                                                  HttpServletRequest request) {
        return success(new PassportAccessTokenRespVO().setAccessToken(StringUtils.uuid(true)).setRefreshToken(StringUtils.uuid(true)).setExpiresTime(new DateTime().toJdkDate()),"用户名为"+loginByUserNameDTO.getUsername()+"的员工登录成功。");
    }

    @PostMapping("/login-by-sms")
    @ApiOperation("手机验证码登陆")
    @RequiresNone
    public CommonResult<PassportAccessTokenRespVO> loginBySms(@Valid PassportLoginBySmsReqVO loginBySmsDTO,
                                                              HttpServletRequest request) {
        return passPortRpc.loginBySms(loginBySmsDTO, HttpUtil.getIp(request));
    }
    @PostMapping("/send-sms-code")
    @ApiOperation("发送手机验证码")
    @RequiresNone
    public CommonResult<Boolean> sendSmsCode(@Valid PassportSendSmsRespVO sendSmsCodeDTO,
                                             HttpServletRequest request) {
        passPortRpc.sendSmsCode(sendSmsCodeDTO, HttpUtil.getIp(request));
        // 返回成功
        return success(true);
    }

    @PostMapping("/refresh-token")
    @ApiOperation("刷新令牌")
    @RequiresNone
    public CommonResult<PassportAccessTokenRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken,
                                                            HttpServletRequest request) {
        return passPortRpc.refreshToken(refreshToken, HttpUtil.getIp(request));
    }

}
